package come.itheima.springbootwebmybatistlias.controller;

import come.itheima.springbootwebmybatistlias.anno.Log;
import come.itheima.springbootwebmybatistlias.pojo.Dept;
import come.itheima.springbootwebmybatistlias.pojo.Result;
import come.itheima.springbootwebmybatistlias.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    //private static Logger log = LoggerFactory.getLogger(DeptController.class);
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)

    @Autowired
    private DeptService deptService;


    //查询部门数据
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        //调用service查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }


    //删除部门数据
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门:{}",id);//{}参数占位符
        deptService.delete(id);
        return Result.success();
    }


    //新增部门
    @Log
    @PostMapping
    public Result insert(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }


    //根据id查询部门
    @GetMapping("/{id}")
    public Result queryDeptById(@PathVariable Integer id) {
        log.info("根据id查询部门数据:{}", id);

        // 注意返回类型是Dept
        Dept deptList = deptService.queryDeptById(id);
        return Result.success(deptList);

    }



    //修改部门数据
    @Log
    @PutMapping
    public Result modifyDept(@RequestBody Dept dept) {
        log.info("修改部门信息");
        deptService.modifyDept(dept);
        return Result.success();
    }


}
